package com.ruoyi.web.controller.store;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.FoodStock;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.mapper.FoodStockMapper;
import com.ruoyi.system.service.IFoodStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2023/8/6
 **/
@RestController
@RequestMapping("/food/store")
public class FoodStockController extends BaseController {
    @Autowired
    private IFoodStockService foodStockService;

    @Resource
    private FoodStockMapper foodStockMapper;

    //这是库存表
    @Log(title = "食材导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FoodStock resource) {
        List<FoodStock> list = foodStockService.selectRestaurantStoreResourceList(resource);
        ExcelUtil<FoodStock> util = new ExcelUtil<FoodStock>(FoodStock.class);
        util.exportExcel(response, list, "食材");
    }

    @GetMapping("/list")
    public TableDataInfo list(FoodStock resource) {
        startPage();
        List<FoodStock> list = foodStockService.selectRestaurantStoreResourceList(resource);
        return getDataTable(list);
    }

    @GetMapping("/allList")
    public AjaxResult allList(FoodStock resource) {
        List<FoodStock> list = foodStockService.selectRestaurantStoreResourceList(resource);
        return AjaxResult.success(list);
    }

    @GetMapping("/getAll")
    public TableDataInfo listAll(FoodStock resource) {
        List<FoodStock> list = foodStockService.selectRestaurantStoreResourceList(resource);
        return getDataTable(list);
    }

    @GetMapping("/latest")
    public TableDataInfo latest(FoodStock resource) {
        List<FoodStock> list = foodStockMapper.selectLatestResourceList(resource);
        return getDataTable(list);
    }

    //("@ss.hasPermi('store:resource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(foodStockService.selectRestaurantStoreResourceById(id));
    }

//    //("@ss.hasPermi('store:resource:add')")
//    @Log(title = "食材管理", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    public AjaxResult add(@Validated @RequestBody CreateResourceDTO createResourceDTO)
//    {
//        Boolean result = foodStockService.foodStockMapper(createResourceDTO);
//        if (result) {
//            return success();
//        }
//        return error();
//    }

    //    //("@ss.hasPermi('store:resource:edit')")
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult edit(@RequestBody FoodStock foodStock) {
        foodStockMapper.updateRestaurantStoreResource(foodStock);
        return AjaxResult.success();
    }

    //("@ss.hasPermi('store:resource:remove')")
    @Log(title = "食材管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(foodStockService.deleteRestaurantStoreResourceById(id));
    }

}
